package cn.iocoder.yudao.module.book.service.follow;

import cn.iocoder.yudao.module.book.dal.dataobject.follow.UserFollowDO;
import cn.iocoder.yudao.module.book.dal.mysql.follow.UserFollowMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户关注关系 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Slf4j
public class UserFollowServiceImpl implements UserFollowService {

    @Resource
    private UserFollowMapper userFollowMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean followUser(Long followerId, Long followeeId) {
        if (followerId.equals(followeeId)) {
            log.warn("用户不能关注自己，userId: {}", followerId);
            return false;
        }

        // 查看是否已存在关注关系
        UserFollowDO existingFollow = userFollowMapper.selectByFollowerAndFollowee(followerId, followeeId);
        
        if (existingFollow != null) {
            // 如果已存在，更新状态为关注
            if (existingFollow.getStatus() == 0) {
                existingFollow.setStatus(1);
                existingFollow.setUpdateTime(LocalDateTime.now());
                userFollowMapper.updateById(existingFollow);
                return true;
            } else {
                // 已经关注了
                return true;
            }
        } else {
            // 创建新的关注关系
            UserFollowDO newFollow = UserFollowDO.builder()
                    .followerId(followerId)
                    .followeeId(followeeId)
                    .status(1)
                    .build();
            newFollow.setCreateTime(LocalDateTime.now());
            newFollow.setUpdateTime(LocalDateTime.now());
            userFollowMapper.insert(newFollow);
            return true;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean unfollowUser(Long followerId, Long followeeId) {
        UserFollowDO existingFollow = userFollowMapper.selectByFollowerAndFollowee(followerId, followeeId);
        
        if (existingFollow != null && existingFollow.getStatus() == 1) {
            // 更新状态为取消关注
            existingFollow.setStatus(0);
            existingFollow.setUpdateTime(LocalDateTime.now());
            userFollowMapper.updateById(existingFollow);
            return true;
        }
        
        return false;
    }

    @Override
    public Boolean isFollowing(Long followerId, Long followeeId) {
        UserFollowDO follow = userFollowMapper.selectByFollowerAndFollowee(followerId, followeeId);
        return follow != null && follow.getStatus() == 1;
    }

    @Override
    public List<Long> getFollowingList(Long userId) {
        return userFollowMapper.selectFolloweeIds(userId);
    }

    @Override
    public List<Long> getFollowersList(Long userId) {
        return userFollowMapper.selectFollowerIds(userId);
    }

    @Override
    public Long getFollowingCount(Long userId) {
        return userFollowMapper.countFollowing(userId);
    }

    @Override
    public Long getFollowersCount(Long userId) {
        return userFollowMapper.countFollowers(userId);
    }

}
